package com.iwm.qa.mdm.view;

import android.content.Intent;
import android.os.Bundle;
import android.support.design.widget.FloatingActionButton;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.util.Log;
import android.view.View;
import android.widget.EditText;
import android.widget.ScrollView;

import com.iwm.qa.mdm.R;
import com.iwm.qa.mdm.http.OkHttp;
import com.iwm.qa.mdm.utils.AttachHttpGetParams;

import java.io.IOException;
import java.util.LinkedHashMap;

import okhttp3.Request;

import static com.iwm.qa.mdm.config.ApiConfig.ADD_DEVICE_INFO;
import static com.iwm.qa.mdm.config.ApiConfig.UPDATE_DEVICE_INFO;
import static com.iwm.qa.mdm.view.DeviceDetailAct.EXTRA_DEVICE_ASSERT_NUM;
import static com.iwm.qa.mdm.view.DeviceDetailAct.EXTRA_DEVICE_BELONGS;
import static com.iwm.qa.mdm.view.DeviceDetailAct.EXTRA_DEVICE_DEPT;
import static com.iwm.qa.mdm.view.DeviceDetailAct.EXTRA_DEVICE_NAME;
import static com.iwm.qa.mdm.view.DeviceDetailAct.EXTRA_DEVICE_RESOLUTION;
import static com.iwm.qa.mdm.view.DeviceDetailAct.EXTRA_OS_TYPE;
import static com.iwm.qa.mdm.view.DeviceDetailAct.EXTRA_REMARK;
import static com.iwm.qa.mdm.view.DeviceDetailAct.EXTRA_VERSION_TYPE;

public class AddEditDeviceActivity extends AppCompatActivity {

    public static final int REQUEST_ADD_DEVICE = 1;

    public static final String EXTRA_DEVICE_OPER_TYPE = "extra_device_oper_type";
    public static final String EXTRA_ADD_DEVICE = "add_device";
    public static final String EXTRA_EDIT_DEVICE = "edit_device";

    public static final int resultCode = 2;
    public String TAG = "AddEditDeviceActivity";

    private EditText mAddEditDeviceName;
    private EditText mAddEditDescription;
    private EditText mAddEditDeviceDept;
    private EditText mAddEditDeviceBelongs;
    private EditText mAddEditOsType;
    private EditText mAddEditOsVersion;
    private EditText mAddEditDeviceResolution;
    private EditText mAddEditDeviceAssertNumber;

    private String mOpType;

    LinkedHashMap<String, String> linkedHashMap = new LinkedHashMap<String, String>();
    private ScrollView mScrollView;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_add_edit_device);
        intiView();

        //新页面接收数据
        Bundle bundle = getIntent().getExtras();

        String deviceName = bundle.getString(EXTRA_DEVICE_NAME);
        String deviceAssetNum = bundle.getString(EXTRA_DEVICE_ASSERT_NUM);
        String osType = bundle.getString(EXTRA_OS_TYPE);
        String versionType = bundle.getString(EXTRA_VERSION_TYPE);
        String deviceResolution = bundle.getString(EXTRA_DEVICE_RESOLUTION);
        String remark = bundle.getString(EXTRA_REMARK);
        String deviceDept = bundle.getString(EXTRA_DEVICE_DEPT);
        String deviceBelongs = bundle.getString(EXTRA_DEVICE_BELONGS);
        mOpType = bundle.getString(EXTRA_DEVICE_OPER_TYPE);

        mAddEditDeviceName.setText(deviceName);
        mAddEditDeviceDept.setText(deviceDept);
        mAddEditDeviceBelongs.setText(deviceBelongs);
        mAddEditDescription.setText(remark);
        mAddEditOsType.setText(osType);
        mAddEditOsVersion.setText(versionType);
        mAddEditDeviceResolution.setText(deviceResolution);
        mAddEditDeviceAssertNumber.setText(deviceAssetNum);
    }

    private void intiView() {
        Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);

        mAddEditDeviceName = (EditText) findViewById(R.id.add_edit_device_title);
        mAddEditDeviceDept = (EditText) findViewById(R.id.add_edit_device_dept);
        mAddEditDeviceBelongs = (EditText) findViewById(R.id.add_edit_device_belongs);
        mAddEditOsType = (EditText) findViewById(R.id.add_edit_os_type);
        mAddEditOsVersion = (EditText) findViewById(R.id.add_edit_os_version);
        mAddEditDeviceAssertNumber = (EditText) findViewById(R.id.add_edit_device_assert_number);
        mAddEditDeviceResolution = (EditText) findViewById(R.id.add_edit_device_resolution);
        mAddEditDescription = (EditText) findViewById(R.id.add_edit_description);
        mScrollView = (ScrollView) findViewById(R.id.scroll_view);
        mScrollView.post(new Runnable() {
            @Override
            public void run() {
                mScrollView.scrollTo(0, 0);
            }
        });

        FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
        fab.setImageResource(R.drawable.ic_done);
        fab.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(final View view) {

                if (EXTRA_ADD_DEVICE.equals(mOpType)) {
                    Log.d(TAG, "onClick: 初始化所有控件value为空");
                    setCurrentVaule();
                    Log.d(TAG, "onClick: 请求设备添加接口");
                    setAddDeviceOkHttp();
                } else {
                    setCurrentVaule();
                    setUpdateDeviceOkHttp();
                }
            }
        });
    }

    private void setUpdateDeviceOkHttp() {
        String reuqestUpdateDeviceURL = AttachHttpGetParams.attachHttpGetParams(UPDATE_DEVICE_INFO, linkedHashMap);
        Log.d(TAG, reuqestUpdateDeviceURL);
        OkHttp.getAsync(reuqestUpdateDeviceURL, new OkHttp.DataCallBack() {
            @Override
            public void requestFailure(Request request, IOException e) {
                Log.d(TAG, "requestFailure: update device 接口请求失败");
            }

            @Override
            public void requestSuccess(String result) throws Exception {
                Log.d(TAG, "requestSuccess: update device 接口请求成功");

//                Snackbar.make(view, "Device has been updated Successfully !", Snackbar.LENGTH_LONG)
//                        .setAction("Action", null).show();

                //请求成功后跳转至设备列表页
                 Intent intent = new Intent(AddEditDeviceActivity.this, MainActivity.class);
                 startActivity(intent);
            }
        });
    }

    private void setCurrentVaule() {
        linkedHashMap.put("deviceName", mAddEditDeviceName.getText().toString());
        linkedHashMap.put("deviceAssetNum", mAddEditDeviceAssertNumber.getText().toString());
        linkedHashMap.put("osType", mAddEditOsType.getText().toString());
        linkedHashMap.put("versionType", mAddEditOsVersion.getText().toString());
        linkedHashMap.put("deviceResolution", mAddEditDeviceResolution.getText().toString());
        linkedHashMap.put("remark", mAddEditDescription.getText().toString());
        linkedHashMap.put("deviceDept", mAddEditDeviceDept.getText().toString());
        linkedHashMap.put("deviceBelongs", mAddEditDeviceBelongs.getText().toString());
    }

    private void setAddDeviceOkHttp() {
        String reuqestAddDeviceURL = AttachHttpGetParams.attachHttpGetParams(ADD_DEVICE_INFO, linkedHashMap);
        Log.d(TAG, reuqestAddDeviceURL);
        OkHttp.getAsync(reuqestAddDeviceURL, new OkHttp.DataCallBack() {
            @Override
            public void requestFailure(Request request, IOException e) {
                Log.d(TAG, "requestFailure: Add device 接口请求失败");
            }

            @Override
            public void requestSuccess(String result) throws Exception {
                Log.d(TAG, "requestSuccess: Add device 接口请求成功");

//                Snackbar.make(view1, "Device has been added Successfully !", Snackbar.LENGTH_LONG)
//                        .setAction("Action", null).show();
                Intent intent = new Intent(AddEditDeviceActivity.this, MainActivity.class);
                startActivity(intent);
                //结束当前的activity的生命周期
                finish();
            }
        });
    }

//    private void setDefaultVaule() {
//        mAddEditDeviceName.setText("");
//        mAddEditDeviceDept.setText("");
//        mAddEditDeviceBelongs.setText("");
//        mAddEditDescription.setText("");
//        mAddEditOsType.setText("");
//        mAddEditOsVersion.setText("");
//        mAddEditDeviceResolution.setText("");
//        mAddEditDeviceAssertNumber.setText("");
//    }

}
